﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;
using Validation.sp2010.ControlTemplates.Validation.sp2010;
using Validation.sp2010.Classes;

namespace Validation.sp2010.CONTROLTEMPLATES.Validation.sp2010
{
    public partial class CreateMail : UserControl
    {
        #region constante

        const string stateIdStep = "IdStep";
        const string stateIsNew = "IsNew";

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Load the field and the user profile Properties to the controls
        /// </summary>
        /// <param name="fields"></param>
        /// <param name="userProfileProperties"></param>
        public void LoadData(List<SPField> fields, List<SPField> userFields, List<CoreProperty> userProfileProperties)
        {

            (Mail_Actor as GetUser).LoadData(userFields, userProfileProperties);
            (Mail_CC as GetUser).LoadData(userFields, userProfileProperties);

            (TextBox_Mail_Body as FormulaText).LoadData(fields,"");
            (TextBox_Mail_Object as FormulaText).LoadData( fields,"");

            ViewState[stateIsNew] = true;

            this.DataBind();
        }

        public void LoadStep(StepWF step, List<SPField> fields, List<SPField> userFields, List<CoreProperty> userProfileProperties)
        {
            TextBox_Mail_Title.Text = step.Title;

            (Mail_Actor as GetUser).LoadData(userFields, userProfileProperties,step.Actor);
            (Mail_CC as GetUser).LoadData(userFields, userProfileProperties,step.CC_Mail);

            (TextBox_Mail_Body as FormulaText).LoadData(fields, step.BodyDisplay);
            (TextBox_Mail_Object as FormulaText).LoadData(fields, step.ObjectDisplay);
            DropDown_StartStep.SelectedIndex = step.IsInitial ? 0 : 1;
            DropDown_FinalStep.SelectedIndex = step.IsFinal ? 0 : 1;


            ViewState[stateIsNew] = false;
            ViewState[stateIdStep] = step.Sequence;

            this.DataBind();
        }

         /// <summary>
        /// Return the Current mail step created null if error
        /// </summary>
        /// <returns></returns>
        public StepWF GetMailStep()
        {

            StepWF stepWf = new StepWF();

            if (!(bool)ViewState[stateIsNew])
            {
                stepWf.Sequence = (int)ViewState[stateIdStep];
            }


            FormulaText objectMail = (TextBox_Mail_Object as FormulaText);



            FormulaText body = (TextBox_Mail_Body as FormulaText);
            GetUser actor = (Mail_Actor as GetUser);
            GetUser mail_cc = (Mail_CC as GetUser);


            StepPrincipal principal = actor.GetPrincipal();
            if (principal == null)
                return null;

            if (!objectMail.VerifyFormule())
                return null;

            if (!body.VerifyFormule())
                return null;


            stepWf.ObjectDisplay = objectMail.DisplayFinalString;
            stepWf.Object = objectMail.InternalFinalString;

            stepWf.Title = TextBox_Mail_Title.Text;

            stepWf.Actor = actor.GetPrincipal();

            stepWf.CC_Mail = mail_cc.GetPrincipal();
            stepWf.Body = body.InternalFinalString;
            stepWf.BodyDisplay = body.DisplayFinalString;
            stepWf.TypeStep = TypeStep.Notfication;
            stepWf.IsFinal = DropDown_FinalStep.SelectedIndex == 0;
            stepWf.IsInitial = DropDown_StartStep.SelectedIndex == 0;
            return stepWf;
        }

    }
}
